package util;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 文件工具类
 * @author Bosen 2024/2/5 14:06
 */
public class FileUtils {

    public static void main(String[] args) throws Exception {
        String path = "d:\\Users\\PC\\Documents\\WPSDrive\\626789192\\WPS云盘\\向蜜鸟\\正在做";
        String demand = "会员列表";
        String branch = "MemberList";
        // 创建文件
        createMdFile(demand, branch, path);
    }

    /**
     * 文件读取
     */
    public static BufferedReader readLocationFile(String filepath) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(filepath);
        return new BufferedReader(new InputStreamReader(fis));
    }

    /**
     * 文件夹读取
     */
    public static File[] readLocationFolder(String filepath) {
        File folder = new File(filepath);
        return folder.listFiles();
    }

    /**
     * 复写本地文件
     */
    public static void overWriteLocationFile(String filepath, String overContext) {
        try {
            PrintStream ps = new PrintStream(filepath);
            ps.print(overContext);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成 Java 文件
     */
    public static void createJavaFile(String filepath, String filepre, String filename, String filesuf,  String context) throws IOException {
        filename = filepath + "\\" + filepre + "\\" + filename + filesuf + ".java";
        File file = new File(filename);
        if(file.exists()) {
            // 文件已存在
            System.out.println("\n此文件已存在: " + filename);
            System.out.print("是否选择覆盖: [y/n] ");
            Scanner scanner = new Scanner(System.in);
            String flag = scanner.next();
            if(!"y".equals(flag)) {
                return;
            }
        }
        file.getParentFile().mkdirs();
        file.createNewFile();
        overWriteLocationFile(file.getAbsolutePath(), context);
        System.out.println("成功创建文件: " + file.getAbsolutePath());
    }
    
    /**
     * 生成 MD 文件
     */
    public static void createMdFile(String demand, String branch, String path) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String filename = demand + "-" + branch;
        File file = new File(path + "/" + sdf.format(new Date()) + "-" + filename + "/" + filename + ".md");
        if(file.exists()) {
            // 文件已存在
            System.out.println("\n此文件已存在: " + filename);
            System.out.print("是否选择覆盖: [y/n] ");
            Scanner scanner = new Scanner(System.in);
            String flag = scanner.next();
            if(!"y".equals(flag)) {
                return;
            }
        }
        file.getParentFile().mkdirs();
        file.createNewFile();
        StringBuilder sb = new StringBuilder();
        // 标题（用于梳理需求逻辑）
        sb.append("### ").append(filename).append("\n")
                .append("- PHP").append("\n")
                .append("- Java").append("\n")
                .append("---").append("\n");
        // SQL（记录上线需要执行的SQL和操作）
        sb.append("### ").append("SQL").append("\n")
                .append("```sql").append("\n")
                .append("```").append("\n")
                .append("---").append("\n");
        // 对接（用于记录与前端对接的接口文档）
        sb.append("### ").append("DOC").append("\n")
                .append("##### ").append("ApiName").append("\n")
                .append("- 接口: ").append("ApiUrl").append("\n")
                .append("- 请求参数: ").append("\n")
                .append("```json").append("\n")
                .append("```").append("\n")
                .append("- 返回参数: ").append("\n")
                .append("```json").append("\n")
                .append("```").append("\n")
                .append("---").append("\n");
        // Untitled
        sb.append("### ").append("Untitled").append("\n")
                .append("```json")
                .append("\n")
                .append("```");
        overWriteLocationFile(file.getAbsolutePath(), sb.toString());
        System.out.println("成功创建文件: " + file.getAbsolutePath());
    }

    /**
     * 首字母大写
     */
    public static String upperCaseName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return  name;
    }

    /**
     * 首字母小写
     */
    public static String lowerCaseName(String name) {
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
        return  name;
    }

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
